﻿using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using RpcStore.Model.DB;
using RpcStore.RemoteModel.Transmit.Model;
using WeDonekRpc.SqlSugar;

namespace RpcStore.DAL.Repository
{
    internal class ServerTransmitConfigDAL : IServerTransmitConfigDAL
    {
        private readonly IRepository<ServerTransmitConfigModel> _BasicDAL;
        public ServerTransmitConfigDAL (IRepository<ServerTransmitConfigModel> dal)
        {
            this._BasicDAL = dal;
        }
        public long[] CheckIsConfig (long schemeId, long[] serverId)
        {
            return this._BasicDAL.Gets(a => a.SchemeId == schemeId && serverId.Contains(a.ServerId), a => a.ServerId);
        }
        public void Add (TransmitAdd add)
        {
            this._BasicDAL.Insert(new ServerTransmitConfigModel
            {
                Id = IdentityHelper.CreateId(),
                SchemeId = add.SchemeId,
                ServerId = add.ServerId,
                TransmitConfig = add.TransmitConfig
            });
        }
        public ServerTransmitConfigModel Get (long id)
        {
            ServerTransmitConfigModel config = this._BasicDAL.Get(a => a.Id == id);
            if (config == null)
            {
                throw new ErrorException("rpc.server.transmit.not.find");
            }
            return config;
        }
        public ServerTransmitConfigModel Get (long schemeId, long serverId)
        {
            return this._BasicDAL.Get(a => a.SchemeId == schemeId && a.ServerId == serverId);
        }
        public long[] GetIds (long schemeId)
        {
            return this._BasicDAL.Gets(a => a.SchemeId == schemeId, a => a.Id);
        }
        public long[] GetIds (long[] schemeId)
        {
            return this._BasicDAL.Gets(a => schemeId.Contains(a.SchemeId), a => a.Id);
        }
        public void Delete (long[] ids)
        {
            if (!this._BasicDAL.Delete(a => ids.Contains(a.Id)))
            {
                throw new ErrorException("rpc.server.transmit.delete.fail");
            }
        }
        public void Delete (long id)
        {
            if (!this._BasicDAL.Delete(a => a.Id == id))
            {
                throw new ErrorException("rpc.server.transmit.delete.fail");
            }
        }
        public void Clear (long schemeId)
        {
            long[] ids = this.GetIds(schemeId);
            if (ids.IsNull())
            {
                return;
            }
            this.Delete(ids);
        }
        public void Clear (long[] schemeId)
        {
            long[] ids = this.GetIds(schemeId);
            if (ids.IsNull())
            {
                return;
            }
            this.Delete(ids);
        }
        public bool CheckIsExists (long schemeId)
        {
            return this._BasicDAL.IsExist(a => a.SchemeId == schemeId);
        }
        public void Set (long id, TransmitConfig[] configs)
        {
            if (!this._BasicDAL.Update(c => new ServerTransmitConfigModel
            {
                TransmitConfig = configs
            }, c => c.Id == id))
            {
                throw new ErrorException("rpc.server.transmit.update.fail");
            }
        }

        public ServerTransmitConfigModel[] Gets (long schemeId, long[] serverId)
        {
            return this._BasicDAL.Gets(a => a.SchemeId == schemeId && serverId.Contains(a.ServerId));
        }
        public ServerTransmitConfigModel[] Gets (long schemeId)
        {
            return this._BasicDAL.Gets(a => a.SchemeId == schemeId);
        }
        public bool CheckIsExists (long schemeId, long serverId)
        {
            return this._BasicDAL.IsExist(a => a.SchemeId == schemeId && a.ServerId == serverId);
        }
    }
}
